package com.gz.moreaseManager.sys.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.gz.moreaseManager.core.modal.AjaxResult;
import com.gz.moreaseManager.core.modal.HttpCode;
import com.gz.moreaseManager.core.modal.PageResult;
import com.gz.moreaseManager.sys.entity.Dict;
import com.gz.moreaseManager.sys.entity.Menu;
import com.gz.moreaseManager.sys.service.IDictService;
import com.gz.moreaseManager.sys.transfer.SysDictVo;
import com.gz.moreaseManager.utils.UtilTool;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.servlet.ModelAndView;

/**
 * <p>
 * 字典表 前端控制器
 * </p>
 *
 * @author wike
 * @since 2019-02-21
 */
@RestController
@RequestMapping("/dict")
public class DictController {

    @Autowired
    private IDictService dictService;

    /**
     * 返回用户列表界面
     *
     * @return
     */
    @GetMapping("/list")
    public ModelAndView listPage() {
        return new ModelAndView("sys/sysDict/list");
    }

    /**
     * 分页查询字典项目
     *
     * @param dictName
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping("/listPage")
    public PageResult<Dict> findAllDictPage(String dictName, Integer pageNum, Integer pageSize) {

        //分页
        Page paramsPage = new Page();
        paramsPage.setCurrent(pageNum);
        paramsPage.setSize(pageSize);
        //构建查询
        Wrapper wrapper = new EntityWrapper<Menu>();
        wrapper.eq("pid",0);
        if( UtilTool.isNotBlank( dictName ) ){
            wrapper.like("name",dictName).or().like("code",dictName);
        }
        Page<Dict> lists = dictService.selectPage(paramsPage, wrapper);
        return new PageResult<>(0, null, lists.getTotal(), lists.getRecords());
    }

    @PostMapping("/addDict")
    public AjaxResult<Boolean> addDict(@RequestBody SysDictVo sysDictVo) {
        if (sysDictVo == null
                || UtilTool.isBlank(sysDictVo.getCode())
                || UtilTool.isBlank(sysDictVo.getName())) {
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");
        }

        //替换属性值
        Boolean success = dictService.addSysDict(sysDictVo);
        return success ? AjaxResult.success() : AjaxResult.build(HttpCode.FAILED, "添加字典信息失败!");
    }

    @PostMapping("/updateDict")
    public AjaxResult<Boolean> updateDict(@RequestBody SysDictVo sysDictVo) {
        if (sysDictVo == null
                || UtilTool.isBlank(sysDictVo.getId())
                || UtilTool.isBlank(sysDictVo.getCode())
                || UtilTool.isBlank(sysDictVo.getName())) {
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");
        }

        Boolean success = dictService.updateSysDict(sysDictVo);
        return success ? AjaxResult.success() : AjaxResult.build(HttpCode.FAILED, "修改字典信息失败!");
    }

    @PostMapping("/delDict")
    public AjaxResult<Boolean> deleteDict(Integer id) {
        if (null == id)
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误");
        Boolean success = dictService.deleteById(id);
        return success ? AjaxResult.success() : AjaxResult.build(HttpCode.FAILED, "删除字典信息失败!");
    }

    @GetMapping("/findDictById")
    public AjaxResult<SysDictVo> findSysDictById(Integer id) {
        if (null == id)
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误");
        SysDictVo sysDictVo = dictService.findDictInfoById(id);
        if (UtilTool.isNotBlank(sysDictVo) && UtilTool.isNotBlank(sysDictVo.getId()))
            return AjaxResult.success(sysDictVo);
        return AjaxResult.build(HttpCode.FAILED, "查询字典信息失败!");
    }

    @GetMapping("/findDictByCode")
    public AjaxResult<SysDictVo> findDictByCode(String code) {
        if (UtilTool.isBlank( code ))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误");
        SysDictVo sysDictVo = dictService.findDictInfoByCode(code);
        if (UtilTool.isNotBlank(sysDictVo) && UtilTool.isNotBlank(sysDictVo.getId()))
            return AjaxResult.success(sysDictVo);
        return AjaxResult.build(HttpCode.FAILED, "查询字典信息失败!");
    }

}
